package xin.marcher.module.evaluate.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xin.marcher.module.common.enums.inventory.InventoryExceptionCode;
import xin.marcher.module.common.exception.BaseBizException;
import xin.marcher.module.evaluate.domain.dto.ItemScoreWeightConfigurationResultDTO;
import xin.marcher.module.evaluate.domain.request.ItemScoreWeightConfigurationRequest;
import xin.marcher.module.evaluate.repository.EvaluateRepository;
import xin.marcher.module.evaluate.service.ItemScoreWeightConfigurationService;

import java.util.Objects;

import static xin.marcher.module.common.constants.evaluate.EvaluateConstants.TOTAL_WEIGHT;

/**
 *
 */
@Service
public class ItemScoreWeightConfigurationServiceImpl implements ItemScoreWeightConfigurationService {

    @Autowired
    private EvaluateRepository evaluateRepository;

    /**
     * 创建商品评分权重
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ItemScoreWeightConfigurationResultDTO createItemScoreWeightConfiguration(ItemScoreWeightConfigurationRequest request) {
        // 参数校验
        checkParams(request);
        // 保存
        long id = evaluateRepository.createItemScoreWeightConfiguration(request);
        // 其他配置项修改为失效
        evaluateRepository.invalidItemScoreWeightConfiguration(id);
        return new ItemScoreWeightConfigurationResultDTO(Boolean.TRUE);
    }

    /**
     * 修改权重值
     *
     * @param request
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ItemScoreWeightConfigurationResultDTO updateItemScoreWeightConfiguration(ItemScoreWeightConfigurationRequest request) {
        // 参数校验
        checkParams(request);
        evaluateRepository.updateItemScoreWeightConfiguration(request);
        return new ItemScoreWeightConfigurationResultDTO(Boolean.TRUE);
    }

    /**
     * 创建商品入参校验
     *
     * @param request
     */
    private void checkParams(ItemScoreWeightConfigurationRequest request) {
        if (Objects.isNull(request)) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getBadCommentCountWeight())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getCommentCountWeight())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getGoodCommentCountWeight())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getLikeCountWeight())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        if (Objects.isNull(request.getSellCountWeight())) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }

        // 权重总和为10
        int sum = request.getBadCommentCountWeight() +
                request.getCommentCountWeight() +
                request.getGoodCommentCountWeight() +
                request.getLikeCountWeight() +
                request.getSellCountWeight();
        if (sum != TOTAL_WEIGHT) {
            throw new BaseBizException(InventoryExceptionCode.PARAM_CHECK_ERROR, InventoryExceptionCode.PARAM_CHECK_ERROR.getErrorCode());
        }
    }
}
